import Vue from "vue";
import Vuex from "vuex";
import Cookies from "js-cookie";
import {
  phoneLogin,
  // getUserInfo,
  logout,
  canSend,
  keepAlive_getToken,
  personInfo,
  resumeList,
  topToken,
} from "@/api/api";
import {
  getToken,
  setToken,
  removeToken,
  // getUserId,
  // setUserId,
  // removeUserId,
} from "@/api/auth";
import { Notify, Toast } from "vant";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: getToken(),
    userInfo: null,
    // userId:getUserId(),
    recruitScrollY: 0,
    canSend: false,
    eduLevels: [
      {
        value: "0",
        label: "初中及以下",
      },
      {
        value: "1",
        label: "高中",
      },
      {
        value: "2",
        label: "中专/中技",
      },
      {
        value: "3",
        label: "大专",
      },
      {
        value: "4",
        label: "本科",
      },
      {
        value: "5",
        label: "硕士",
      },
      {
        value: "6",
        label: "MBA/EMBA",
      },
      {
        value: "7",
        label: "博士",
      },
    ],
    experience: [
      {
        value: "0,999",
        label: "不限",
      },
      {
        value: "0,0",
        label: "无经验",
      },
      {
        value: "0,1",
        label: "1年以下",
      },
      {
        value: "1,3",
        label: "1-3年",
      },
      {
        value: "3,5",
        label: "3-5年",
      },
      {
        value: "5,10",
        label: "5-10年",
      },
      {
        value: "10,99",
        label: "10年以上",
      },
    ],
  },
  getters: {
    recruitScrollY: (state) => state.recruitScrollY,
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_INFO: (state, info) => {
      state.userInfo = info;
    },
    // SET_USERID: (state, id) => {
    // 	state.userId = id
    // },
    changeRecruitScrollY(state, recruitScrollY) {
      state.recruitScrollY = recruitScrollY;
    },
    SET_CANSEND: (state, bl) => {
      state.canSend = bl;
    },
  },
  actions: {
    login({ commit }, form) {
      return new Promise((resolve, reject) => {
        // 登录接口
        phoneLogin(form)
          .then((res) => {
            if (res.result?.token) {
              //存储登录状态token
              localStorage.setItem("loginStatus", 1);
              localStorage.setItem("X-Access-Token", res.result?.token);
              localStorage.setItem("userId", res.result?.id);
              commit("SET_TOKEN", res.result?.token);
              // commit('SET_USERID', res.result?.id)
              setToken(res.result?.token);
              // setUserId(res.result?.id)
              keepAlive_getToken({ sence: "H5", dayNum: 30 }).then(
                (response) => {
                  Cookies.set("alive_token", response.message, { expires: 30 });
                  resolve();
                }
              );
              if (!form.loginType) {
                Notify({
                  type: "success",
                  message: "登录成功！",
                });
              }
              //获取用户信息
              personInfo({
                id: res.result?.id,
              }).then((res) => {
                if (res.code === 200) {
                  localStorage.setItem("userInfo", JSON.stringify(res.result));
                  //简历列表
                  resumeList().then((res) => {
                    if (res.code === 200) {
                      let list = res.result?.records;
                      for (let i in list) {
                        if (list[i].lanType === 0 && list[i].type === 0) {
                          localStorage.setItem("resumeId", list[i].id);
                        }
                      }
                    }
                  });
                }
              });
            } else {
              Toast(res.message);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 顶端登录
    loginTop({ commit }, { token, cb }) {
      topToken(Object.assign({}, { token })).then((res) => {
        if (res.code !== 200) {
          Toast(res.message);
          return;
        }
        localStorage.setItem("userInfo", JSON.stringify(res.result));
        cb && cb();
      });
    },
    // remove token
    resetToken({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        // commit('SET_USERID', '')
        removeToken();
        // removeUserId()
        resolve();
      });
    },
    // getInfo({
    // 	commit,
    // 	state
    // }) {
    // 	return new Promise((resolve, reject) => {
    // 		getUserInfo({id:state.userId}).then(response => {
    // 			console.log(response.result)
    // 			commit('SET_INFO', response.result)
    // 			resolve(response.result)
    // 		}).catch(error => {
    // 			reject(error)
    // 		})
    // 	})
    // },
    canSend({ commit, state }) {
      return new Promise((resolve, reject) => {
        canSend()
          .then((response) => {
            const canSend =
              response.result.eduFlag &&
              response.result.infoFlag &&
              response.result.intentionFlag &&
              response.result.workFlag;
            commit("SET_CANSEND", canSend);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
});
